﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Eflong.Config;
using Eflong.Convert;
using Eflong.KJFiles;
using Eflong.MonitoringLib.MonitoringUnit;
using Eflong.SocketClient;
using Eflong.Structure;
using Eflong.Utility;

namespace KWBridge
{
    public class KJBridge : Client , IBridge
    {
        public KJXXN_Files kjxx { get; set; }

        public KJXXN_Files kjxx2 { get; set; }

        /// <summary>
        /// 矿井Id
        /// </summary>
        public UInt32 Id { get; set; }

        /// <summary>
        /// 矿井名称
        /// </summary>
        public string CoalMineName { get; set; }

        public string LocalIPAddr { get; set; }

        /// <summary>
        /// 演示方式
        /// </summary>
        public bool IsDemo { get; set; }

        /// <summary>
        /// 测点定义改变
        /// </summary>
        public bool IsDefinitionChanged = false;

        /// <summary>
        /// 测点定义改变2
        /// </summary>
        public bool IsDefinitionChanged2 = false;

        /// <summary>
        /// 实时数据改变
        /// </summary>
        public bool IsDataChanged = false;

        /// <summary>
        /// 实时数据改变2
        /// </summary>
        public bool IsDataChanged2 = false;

        /// <summary>
        /// 实时数据不变化
        /// </summary>
        public bool IsDataNotChanged = false;

        /// <summary>
        /// 实时数据不变化2
        /// </summary>
        public bool IsDataNotChanged2 = false;

        /// <summary>
        /// 无法读取网络文件
        /// </summary>
        public bool IsNotReadNetFiles = false;

        /// <summary>
        /// 无法读取网络文件2
        /// </summary>
        public bool IsNotReadNetFiles2 = false;

        public bool IsExceptionChanged = false;

        public KJBridge( KWBridgeConfig cfg , Encoding encoding )
            : this( cfg.Name , cfg.Id , cfg.KJSysParas.KJType , cfg.KJSysParas.KJPath , cfg.KJSysParas.DevFile , cfg.KJSysParas.RtDataFile , encoding , cfg.KJSysParas.KJDigitalStates_dic , cfg.BackupDir )
        {
            PoolingInterval = ( cfg.Interval - 0.5f );
        }

        public KJBridge( KWBridgeConfig cfg , Encoding encoding , Encoding encoding2 )
            : this( cfg.Name , cfg.Id , cfg.KJSysParas.KJType , cfg.KJSysParas.KJPath , cfg.KJSysParas.DevFile , cfg.KJSysParas.RtDataFile , cfg.Postfix , cfg.KJSysParas2.KJType , cfg.KJSysParas2.KJPath , cfg.KJSysParas2.DevFile , cfg.KJSysParas2.RtDataFile , cfg.Postfix2 , encoding , encoding2 , cfg.KJSysParas.KJDigitalStates_dic , cfg.BackupDir , cfg.KJSysParas2.KJDigitalStates_dic , cfg.BackupDir2 )
        {
            PoolingInterval = ( cfg.Interval - 0.5f );
        }

        /// <summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="devFileName"></param>
        /// <param name="dataFileName"></param>
        /// <param name="log"></param>
        /// <param name="encoding"></param>
        /// <param name="dic"></param>
        /// <param name="backupDir"></param>
        /// </summary>
        public KJBridge( string name , UInt32 id , string type , string path , string devFileName , string dataFileName , Encoding encoding , Dictionary<string , int> dic , string backupDir )
            : base( )
        {
            KJXX_Types kjtype = KJXX_Files.GetKJXX_Type( type );
            kjxx = new KJXXN_Files( kjtype , path , devFileName , dataFileName , encoding ) { nId = id , Postfix = "" };

            IsNotReadNetFiles2 = false;

            InitParam( name , id );

            InitCoalMineManager( );

            SetCommonDigitalStates( kjxx , dic );
            try
            {
                kjxx.FileBackup = new FileBackup( backupDir );
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "new FileBackup Error: " + ex.Message );
            }
        }

        /// <summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="devFileName"></param>
        /// <param name="dataFileName"></param>
        /// <param name="postfix"></param>
        /// <param name="type2"></param>
        /// <param name="path2"></param>
        /// <param name="devFileName2"></param>
        /// <param name="dataFileName2"></param>
        /// <param name="postfix2"></param>
        /// <param name="log"></param>
        /// <param name="encoding"></param>
        /// <param name="encoding2"></param>
        /// <param name="dic"></param>
        /// <param name="backupDir"></param>
        /// <param name="dic2"></param>
        /// <param name="backupDir2"></param>
        /// </summary>
        public KJBridge( string name , UInt32 id , string type , string path , string devFileName , string dataFileName , string postfix , string type2 , string path2 , string devFileName2 , string dataFileName2 , string postfix2 , Encoding encoding , Encoding encoding2 , Dictionary<string , int> dic , string backupDir , Dictionary<string , int> dic2 , string backupDir2 )
            : base( )
        {
            KJXX_Types kjtype = KJXX_Files.GetKJXX_Type( type );
            kjxx = new KJXXN_Files( kjtype , path , devFileName , dataFileName , encoding );
            kjxx.nId = id;
            kjxx.Postfix = postfix;

            KJXX_Types kjtype2 = KJXX_Files.GetKJXX_Type( type2 );
            kjxx2 = new KJXXN_Files( kjtype2 , path2 , devFileName2 , dataFileName2 , encoding2 );
            kjxx2.nId = id;
            kjxx2.Postfix = postfix2;
            if ( string.IsNullOrEmpty( dataFileName2 ) || string.IsNullOrEmpty( devFileName2 ) )
            {
                IsNotReadNetFiles2 = false;
            }

            InitParam( name , id );

            InitCoalMineManager( );

            SetCommonDigitalStates( kjxx , dic );

            kjxx.FileBackup = new FileBackup( backupDir );

            SetCommonDigitalStates( kjxx2 , dic2 );

            try
            {
                kjxx2.FileBackup = new FileBackup( backupDir2 );
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "new FileBackup2 Error: " + ex.Message );
            }
        }

        private void InitParam( string name , UInt32 id )
        {
            networkMode = NetworkModes.Bridge;
            PoolingInterval = PoolingInterval < 5 ? 9 : PoolingInterval;

            Id = id;
            CoalMineName = name;
            LocalIPAddr = "127.0.0.1";
        }

        private void InitCoalMineManager( )
        {
            BaseCoalMineUnit coalMineUnit = new BaseCoalMineUnit( );
            coalMineUnit.m_nId = Id;
            coalMineUnit.m_strName = CoalMineName;
            coalMineUnit.m_strIPAddr = LocalIPAddr;
            base.manager.AddCoalMineUnit( coalMineUnit );
        }

        /// <summary>
        /// 设置接口程序对应的开关量状态的对应数值关系
        /// <param name="kjfile"></param>
        /// <param name="dic"></param>
        /// </summary>
        public void SetCommonDigitalStates( KJXXN_Files kjfile , Dictionary<string , int> dic )
        {
            if ( kjfile == null || dic == null )
            {
                throw new Exception( "没有正常设置接口程序的参数" );
            }

            kjfile.CleanupDigitalStates( );

            foreach ( string type in dic.Keys )
            {
                DigitalState state = new DigitalState( type );
                kjfile.AddDigitalState( state );
                if ( dic[ type ] == 0 )
                {
                    // 数值0 对应状态0 ;
                    // State0 对应正常状态
                    state.bState0 = true;
                }
                else
                {
                    // 数值0 对应状态1
                    state.bState0 = false;
                }
                if ( type == "风机" )
                {
                    DigitalState s;
                    s = new DigitalState( "主扇" );
                    s.bState0 = state.bState0;
                    kjfile.AddDigitalState( s );

                    s = new DigitalState( "局扇" );
                    s.bState0 = state.bState0;
                    kjfile.AddDigitalState( s );
                }
            }
        }

        /// <summary>
        /// <param name="user"></param>
        /// <param name="passward"></param>
        /// </summary>
        public void LoginForBridge( string user , string passward )
        {
            networkMode = NetworkModes.Bridge;
            Login( user , passward , LoginStruct.VCS_MT_LOGIN , LoginStruct.LOGIN_BRIDGE );
        }

        /// <summary>
        /// 按网桥方式退出
        /// <param name="user"></param>
        /// <param name="passward"></param>
        /// </summary>
        public void LogoutForBridge( string user , string passward )
        {
            Logout( user , passward , LoginStruct.VCS_MT_LOGOUT , LoginStruct.LOGOUT_BRIDGE );
        }

        public override void Stop( )
        {
            if ( Connected && Logined )
            {
                SetHaltForQuit( );
                SendDataUpdateRequest( );
                Thread.Sleep( 500 );
            }

            base.Stop( );
        }

        /// <summary>
        /// 发生单步请求
        /// </summary>
        public void SendStepRequest( )
        {
            SendBridgeUpdateRequest( );
        }

        /// <summary>
        /// 改写网桥更新请求
        /// </summary>
        protected override void SendBridgeUpdateRequest( )
        {
            if ( IsDefinitionChanged || ReLogined )
            {
                m_checkSendDataAck.Reset( );
                if ( SendDefinitionRequest( ) )
                {
                    DispatchMessage( SEND_DEFINITION );  //向服务器发送测点定义信息

                    // 等待服务器发送请求数据应答信号
                    m_checkSendDataAck.WaitOne( 7500 , false );
                }
                Thread.Sleep( 500 );
            }

            if ( IsDataChanged && !IsDataNotChanged || ReLogined )
            {
                SendDataUpdateRequest( );
                DispatchMessage( SEND_DATAS );//向服务器发送实时数据信息;
            }
            if ( IsDataNotChanged )
            {
                SendNoDataChangedRequest( );
                DisptchExceptionMessage( FILE_NOTCHANGED );
                IsDataNotChanged = false;
            }
        }

        /// <summary>
        /// 转换测点定义
        /// </summary>
        public virtual bool ConvertDefinition( )
        {
            bool bChanged = false;
            try
            {
                if ( kjxx.ParseDefinition( ) )
                {
                    bChanged = true;
                }
                if ( ( kjxx.DoubleDevFiles && kjxx.ParseDefinition2( ) ) )
                {
                    bChanged = true;
                }
                if ( bChanged )
                {
                    BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );

                    KJXXNToMonitoring.KJXXN_DevFileToCoalMineUnitManager( kjxx.devFile , ref coalMineUnit , 1 , kjxx.Postfix );
                }
                IsNotReadNetFiles = false;
            }
            catch ( Exception ex )
            {
                DisptchException( ex );
                throw new Exception( ex.Message );
            }
            return bChanged;
        }

        /// <summary>
        /// 转换测点定义 2
        /// </summary>
        public bool ConvertDefinition2( )
        {
            bool bChanged = false;
            try
            {
                if ( kjxx2.ParseDefinition( ) )
                {
                    bChanged = true;
                }
                if ( ( kjxx2.DoubleDevFiles && kjxx2.ParseDefinition2( ) ) )
                {
                    bChanged = true;
                }
                if ( bChanged )
                {
                    BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                }
                IsNotReadNetFiles2 = false;
            }
            catch ( Exception ex )
            {
                DisptchException( ex );
            }

            return bChanged;
        }

        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="ex"></param>
        public void DisptchException( Exception ex )
        {
            if ( ex.Message.Contains( FILE_NOTEXISETS )
                || ex.Message.Contains( FILE_NOTBEREAD )
                || ex.Message.Contains( FILE_NOITEMS ) )
            {
                IsNotReadNetFiles = true;

                DisptchExceptionMessage( ex.Message );
            }
            else
            {
                DisptchExceptionMessage( ex.Message );
            }
        }

        static private DateTime tmChanged = DateTime.Now;

        /// <summary>
        /// 转换实时数据
        /// </summary>
        public virtual bool ConvertData( )
        {
            bool bChaned = false;
            try
            {
                if ( !IsDemo )
                {
                    bChaned = kjxx.ParseRealData( );

                    if ( ( kjxx.DoubleRealDataFiles ) )
                    {
                        bChaned |= kjxx.ParseRealData2( );
                    }
                    IsNotReadNetFiles = false;
                }
                else
                {
                    kjxx.ParseDemoData( );
                    bChaned = true;
                }
                if ( bChaned )
                {
                    IsDataNotChanged = false;
                    tmChanged = DateTime.Now;
                    if ( kjxx.kjType == KJXX_Types.KJXY || kjxx.kjType == KJXX_Types.KJ4N )
                    {
                        // 解析参数文件
                        try
                        {
                            kjxx.ParseParas( );
                        }
                        catch ( Exception ex )
                        {
                            LogWrapper.Logger.Error( ex.Message );
                        }
                    }
                    BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                    KJXXNToMonitoring.KJXXN_RtDataFileToCoalMineUnitManager( kjxx.rtData , ref coalMineUnit , 1 , kjxx.Postfix );
                }
                else
                {
                    if ( DateTime.Now.Subtract( tmChanged ).TotalMinutes >= DataChangedInterval )
                    {
                        tmChanged = DateTime.Now;
                        BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );

                        // 取消数据文件不变化，就设置网络中断的标志
                        bChaned = true;// coalMineUnit.SetDatasNotChanged();
                        if ( bChaned )
                        {
                            IsDataNotChanged = true;
                            DisptchExceptionMessage( kjxx.RealDataFileName + " : " + FILE_NOTCHANGED );
                        }
                    }
                }
            }
            catch ( Exception ex )
            {
                DisptchException( ex );
                throw new Exception( ex.Message );
            }

            return bChaned;
        }

        static private DateTime tmChanged2;

        /// <summary>
        /// 转换实时数据
        /// </summary>
        public bool ConvertData2( )
        {
            bool bChaned = false;
            try
            {
                if ( !IsDemo )
                {
                    if ( kjxx2.IsDataChangedFileTimeChanged( ) && kjxx2.ParseRealData( ) )
                    {
                        bChaned = true;
                    }
                    if ( ( kjxx2.DoubleRealDataFiles && kjxx2.IsDataChangedFileTimeChanged2( ) && kjxx2.ParseRealData2( ) ) )
                    {
                        bChaned = true;
                    }
                    IsNotReadNetFiles2 = false;
                }
                else
                {
                    kjxx2.ParseDemoData( );
                    bChaned = true;
                }
                if ( bChaned )
                {
                    IsDataNotChanged2 = false;
                    tmChanged2 = DateTime.Now;
                    if ( kjxx2.kjType == KJXX_Types.KJXY || kjxx2.kjType == KJXX_Types.KJ4N )
                    {
                        // 解析参数文件
                        try
                        {
                            kjxx2.ParseParas( );
                        }
                        catch ( Exception ex )
                        {
                            System.Diagnostics.Debug.WriteLine( "kjxx.ParseParas2() err" + ex.Message );
                        }
                    }
                    BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                    KJXXNToMonitoring.KJXXN_RtDataFileToCoalMineUnitManager( kjxx2.rtData , ref coalMineUnit , 2 , kjxx2.Postfix );
                }
                else
                {
                    if ( DateTime.Now.Subtract( tmChanged2 ).TotalMinutes >= DataChangedInterval )
                    {
                        tmChanged2 = DateTime.Now;
                        BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );

                        // 取消数据文件不变化，就设置网络中断的标志
                        bChaned = true;// coalMineUnit.SetDatasNotChanged();
                        if ( bChaned )
                        {
                            IsDataNotChanged2 = true;
                            DisptchExceptionMessage( kjxx2.RealDataFileName + " : " + FILE_NOTCHANGED );
                        }
                    }
                }
            }
            catch ( Exception ex )
            {
                DisptchException( ex );
            }

            return bChaned;
        }

        /// <summary>
        /// 发生测点定义信息
        /// </summary>
        /// <returns></returns>
        public bool SendDefinitionRequest( )
        {
            bool bResult = false;
            try
            {
                BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                BaseMonitoringUnit[ ] units = coalMineUnit.GetNoDeletesMonitoringUnits( ).ToArray( );
                if ( units.Length > 0 )
                {
                    DefinitionUpdateStruct aDefinitionUpdateStruct = new DefinitionUpdateStruct( );
                    aDefinitionUpdateStruct.baseStruct.m_nCoalMineId = ( uint )Id;
                    aDefinitionUpdateStruct.baseStruct.m_dwNumOfDef = ( uint )units.Length;
                    aDefinitionUpdateStruct.baseStruct.m_dwBufSize = aDefinitionUpdateStruct.dwBufSize( );
                    aDefinitionUpdateStruct.baseStruct.m_dwMethodType = VizClientServer.VCS_MT_UPDATEDEFINITION;
                    aDefinitionUpdateStruct.m_MonUnitDef = new DefinitionStruct[ units.Length ];
                    for ( int i = 0; i < units.Length; i++ )
                    {
                        BaseMonitoringUnit item = units[ i ];
                        DefinitionStruct aDef = new DefinitionStruct( );

                        item.ConvertDefToStruct( ref aDef );
                        aDefinitionUpdateStruct.m_MonUnitDef[ i ] = aDef;
                    }

                    Packet packetRequest = new Packet( aDefinitionUpdateStruct.dwBufSize( ) , aDefinitionUpdateStruct.GetByteArray( ) );
                    SendRequst( packetRequest , VCS_MT_TYPES.VCS_MT_UPDATEDEFINITION );
                    packetRequest.Dispose( );
                    bResult = true;
                }
                else
                {
                    DisptchExceptionMessage( FILE_NOITEMS );
                }
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "SendDefinitionRequest: " + ex.Message , ex );
            }

            return bResult;
        }

        /// <summary>
        /// 发送实时数据信息
        /// </summary>
        /// <returns></returns>
        public bool SendDataUpdateRequest( )
        {
            bool bResult = false;
            try
            {
                BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                BaseMonitoringUnit[ ] units = coalMineUnit.GetNoDeletesMonitoringUnits( ).ToArray( );
                if ( units.Length > 0 )
                {
                    DataUpdateStruct aDataUpdateStruct = new DataUpdateStruct( );//DataUpdateDemo();
                    aDataUpdateStruct.baseStruct.m_nCoalMineId = ( uint )Id;
                    aDataUpdateStruct.baseStruct.m_dwNumOfData = ( uint )units.Length;
                    aDataUpdateStruct.baseStruct.m_dwMethodType = VizClientServer.VCS_MT_UPDATEDATA;
                    aDataUpdateStruct.m_MonUnitData = new MonUnitData[ units.Length ];
                    aDataUpdateStruct.baseStruct.m_dwBufSize = ( uint )aDataUpdateStruct.dwBufSize( );

                    for ( int i = 0; i < units.Length; i++ )
                    {
                        BaseMonitoringUnit item = units[ i ];
                        MonUnitData aData = new MonUnitData( );
                        item.ConvertDataToStruct( ref aData );
                        aDataUpdateStruct.m_MonUnitData[ i ] = aData;
                    }

                    Packet packetRequest = new Packet( aDataUpdateStruct.dwBufSize( ) , aDataUpdateStruct.GetByteArray( ) );
                    SendRequst( packetRequest , VCS_MT_TYPES.VCS_MT_UPDATEDATA );
                    packetRequest.Dispose( );
                    bResult = true;
                }
                else
                {
                    DisptchExceptionMessage( FILE_NOITEMS );
                }
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "SendDataRequest: " + ex.Message , ex );
            }

            return bResult;
        }

        /// <summary>
        /// 发送无数据变化的实时数据信息
        /// </summary>
        /// <returns></returns>
        public bool SendNoDataChangedRequest( )
        {
            bool bResult = false;
            try
            {
                BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                coalMineUnit.SetDatasNotChanged( );

                SendDataUpdateRequest( );
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "SendNoDataChangedRequest: " + ex.Message , ex );
            }

            return bResult;
        }

        private static bool bFirstNotReadNetFiles = true;

        private static bool bFirstNotReadNetFiles2 = true;

        /// <summary>
        /// 首先进行本地联网信息的读取
        /// </summary>
        override protected void LocalUpdate( )
        {
            try
            {
                IsDefinitionChanged = ConvertDefinition( );
                if ( kjxx2 != null )
                {
                    IsDefinitionChanged2 = ConvertDefinition2( );
                }
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "ConvertDefinition() : " + ex.Message , ex );
                return;
            }

            try
            {
                if ( IsDefinitionChanged || IsDefinitionChanged2 )
                {
                    Thread.Sleep( 500 );
                }
                IsDataChanged = ConvertData( );
                if ( kjxx2 != null )
                {
                    IsDataChanged2 = ConvertData2( );
                }
            }
            catch ( Exception ex )
            {
                LogWrapper.Logger.Error( "ConvertData() : " + ex.Message );
            }

            if ( IsDefinitionChanged || IsDefinitionChanged2 )
            {
                if ( this.onDefinitionChanged != null )
                {
                    this.onDefinitionChanged( this , new MessageEventArgs( Id ) );
                }
            }
            else
            {
                if ( IsNotReadNetFiles )
                {
                    Set_Break_For_NotReadNetFiles( );
                }
                if ( IsNotReadNetFiles2 )
                {
                    Set_Break_For_NotReadNetFiles2( );
                }
                if ( IsDataChanged || IsDataChanged2 )
                {
                    if ( this.onDataChanged != null )
                    {
                        this.onDataChanged( this , new MessageEventArgs( Id ) );
                    }
                }
            }
        }

        private void Set_Break_For_NotReadNetFiles( )
        {
            if ( bFirstNotReadNetFiles && IsNotReadNetFiles )
            {
                bFirstNotReadNetFiles = false;
                IsDataChanged = true;
                BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                coalMineUnit.Set_FILE_NOTBEREAD( );
            }
            if ( !IsNotReadNetFiles )
            {
                bFirstNotReadNetFiles = true;
            }
        }

        private void SetHaltForQuit( )
        {
            IsDataChanged = true;
            BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
            coalMineUnit.HaltAllMonitoringUnits( );
        }

        private void Set_Break_For_NotReadNetFiles2( )
        {
            if ( bFirstNotReadNetFiles2 && IsNotReadNetFiles2 )
            {
                bFirstNotReadNetFiles2 = false;
                IsDataChanged2 = true;
                BaseCoalMineUnit coalMineUnit = base.manager.GetCoalMineUnit( Id );
                coalMineUnit.Set_FILE_NOTBEREAD2( );
            }
            if ( !IsNotReadNetFiles2 )
            {
                bFirstNotReadNetFiles2 = true;
            }
        }

        private DateTime tmFileCreate;

        /// <summary>
        /// 判断联网的数据文件是否进行修改了
        /// </summary>
        /// <returns></returns>
        protected override bool IsFileTimeChanged( )
        {
            bool bChanged = false;
            if ( IsNotReadNetFiles )
            {
                // 无法读取网络文件时，先延迟30秒，再读取文件，进行判断
                Thread.Sleep( 9 * 1000 );
            }
            try
            {
                if ( kjxx.rtData is Kj143RtData )
                {
                    bChanged = IsKj143RtDataFileChanged( );
                    IsNotReadNetFiles = false;
                }
                else
                {
                    DateTime tm = System.IO.File.GetLastWriteTime( kjxx.DataChangedFileName );
                    double d = tm.Subtract( tmFileCreate ).TotalSeconds;
                    tmFileCreate = tm;
                    if ( d > 0.1 )
                    {
                        bChanged = true;
                    }
                    IsNotReadNetFiles = false;
                }
            }
            catch ( Exception ex )
            {
                Debug.WriteLine( "IsFileTimeChanged err " + ex.Message );
                IsNotReadNetFiles = true;
            }

            return bChanged;
        }

        private bool IsKj143RtDataFileChanged( )
        {
            var kj143RtData = ( Kj143RtData )kjxx.rtData;

            return kj143RtData.IsFileTimeChanged( );
        }

        private DateTime tmFileCreate2;

        /// <summary>
        /// 判断联网的数据文件是否进行修改了
        /// </summary>
        /// <returns></returns>
        protected override bool IsFileTimeChanged2( )
        {
            bool bChanged = false;
            if ( kjxx2 == null || string.IsNullOrEmpty( kjxx2.DataChangedFileName ) )
            {
                return false;
            }
            if ( IsNotReadNetFiles2 )
            {
                // 无法读取网络文件时，先延迟30秒，再读取文件，进行判断
                Thread.Sleep( 9 * 1000 );
            }
            try
            {
                DateTime tm = System.IO.File.GetLastWriteTime( kjxx2.DataChangedFileName );
                double d = tm.Subtract( tmFileCreate2 ).TotalSeconds;
                tmFileCreate2 = tm;
                if ( d > 1 )
                {
                    bChanged = true;
                }
                IsNotReadNetFiles2 = false;
            }
            catch ( Exception ex )
            {
                Debug.WriteLine( "IsFileTimeChanged2 err " + ex.Message );
                IsNotReadNetFiles2 = true;
            }

            return bChanged;
        }
    }
}